﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI;

namespace QT.Web
{
    /// <summary>
    /// 模板操作类
    /// </summary>
    public class BasePage : System.Web.UI.Page
    {
        /// <summary>
        /// 不带参构造函数
        /// </summary>
        public BasePage()
        {
            Control cc1 = LoadControl(Target());
            Page page = HttpContext.Current.CurrentHandler as Page;
            page.FindControl("masterpage").Controls.Add(cc1);
        }

        /// <summary>
        /// 带参构造函数
        /// </summary>
        /// <param name="mytarget">控件地址对应的参数名</param>
        public BasePage(string mytarget)
        {
            Control cc1 = LoadControl(Target(mytarget));
            Page page = HttpContext.Current.CurrentHandler as Page;
            page.FindControl("masterpage").Controls.Add(cc1);
        }

        /// <summary>
        /// 获取控件地址
        /// </summary>
        /// <returns>用户控件URL</returns>
        public string Target()
        {
            string url = System.Web.HttpContext.Current.Request.QueryString["Target"];
            if (!string.IsNullOrEmpty(url))
            {
                if (url.IndexOf("/") != 0)
                {
                    url = "~/" + url;
                }
                if (!url.ToUpper().EndsWith("ASCX"))
                {
                    url += ".ascx";
                }
            }

            if (!System.IO.File.Exists(Server.MapPath(url)))
            {
                throw (new Exception("网页" + url + "不存在！"));
            }

            return url;
        }

        /// <summary>
        /// 获取控件地址
        /// </summary>
        /// <param name="mytarget">控件地址对应的参数名</param>
        /// <returns>用户控件URL</returns>
        public string Target(string mytarget)
        {
            string url = System.Web.HttpContext.Current.Request.QueryString[mytarget];
            if (!string.IsNullOrEmpty(url))
            {
                if (url.IndexOf("/") != 0)
                {
                    url = "~/" + url;
                }
                if (!url.ToUpper().EndsWith("ASCX"))
                {
                    url += ".ascx";
                }
            }

            if (!System.IO.File.Exists(Server.MapPath(url)))
            {
                throw (new Exception("网页" + url + "不存在！"));
            }

            return url;
        }
    }
}
